#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>   // close 
#include <fcntl.h>
#include <string.h>

#define SIZE 128

int main()
{
  // test 
  //close(1);
  //int fd  = open("file.txt",O_WRONLY | O_CREAT, 0644);
  //if(fd < 0){
  //  perror("open");
  //  return 1;
  //}
  //printf("fd : %d\n", fd);
  ////fflush(stdout); //刷新缓冲区
  //int fd1  = open("file.txt",O_WRONLY | O_CREAT, 0644);
  //printf("fd1 : %d\n", fd1);
  //int fd2  = open("file.txt",O_WRONLY | O_CREAT, 0644);
  //printf("fd2 : %d\n", fd2);
  //
  //close (fd);

  //test 0 
  //char buf[1024];
  //ssize_t s = read(0, buf, sizeof(buf));
  //if(s > 0){
  //  buf[s] = 0;
  //  write(1, buf, strlen(buf));
  //  write(2, buf, strlen(buf));
  //}
  
 
  // **********************
  //  system IO wrtie test 
  // *********************
  //umask(0);
  //int fd = open("file.txt",O_WRONLY | O_CREAT  , 0644);
  ////int fd = open("file.txt",O_RDONLY);
  //if(fd<0){
  //  perror("open error\n");
  //  return 0;
  //}
  ////printf("hello print: %d\n", fd);
  //
  //int count = 4;
  //const char *msg = "hello yumoz csdn\n";
  //while(count--){
  //  write(fd, msg, strlen(msg));
  //}
  //close(fd);

  // **********************
  //  system IO read  test 
  // *********************
  int fd = open("file.txt",O_RDONLY);
  if(fd<0){
    perror("open error\n");
    return 0;
  }

  const char *msg = "hello csdn\n";
  char buf[1024];
  while(1){
    ssize_t s = read(fd, buf, strlen(msg));
    if(s > 0){
      printf("%s", buf);
    }
    else {
      break;
    }
    // write(1, &c, 1);
  }
  close(fd);

  return 0;
}
